package com.zzptc.sgz.db;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbImpl implements IDb{
    Connection conn;
    private String DRIVER="jdbc:mysql://localhost:3306/taotaobuy?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true";

    public DbImpl() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Connection openConn() throws SQLException {
       conn= DriverManager.getConnection(DRIVER, "root", "123456");
        return conn;
    }

    @Override
    public void closeConn() throws SQLException {
        if(conn!=null&&!conn.isClosed()){
           conn.close();
        }

    }

    public static void main(String[] args) {
        DbImpl db = new DbImpl();
        try {
            System.out.println(db.openConn().isClosed());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

//   private String DRIVER="jdbc:mysql://localhost:3306/mysql?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true";
//   private  String USER="root";
//   private  String PASSWORD="123456";
//   private  Connection connection;
//    public DbImpl() {
//        //加载驱动
//        try {
//            Class.forName("com.mysql.jdbc.Driver");
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
//    }
//    @Override
//    public Connection openConn() throws SQLException {
//        connection= DriverManager.getConnection(DRIVER, USER, PASSWORD);
//        return connection;
//    }
//    @Override
//    public void closeConn() throws SQLException {
////        if(connection!=null&&!connection.isClosed()){
////            connection.close();
////        }
//    }
//    public static void main(String[] args) {
//        DbImpl db = new DbImpl();//调用了构造方法
//        try {
//            System.out.println(db.openConn().isClosed());
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
//    }
}



